﻿using DBModels;
using Method.DateTime;
using Models.operResult;
using Models.VModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Api.WDS.Methods
{
    public class ArticleMet
    {
        /// <summary>
        /// 插入文章
        /// </summary>
        /// <param name="uid"></param>
        /// <param name="v_Publish"></param>
        /// <returns></returns>
        public OperResult CreatetArticle(int uid, V_Publish v_Publish)
        {
            var article = new ArticleList()
            {
                ReleaseId = uid,
                Time = GetDateID.getDateId,
                Title = v_Publish.Title,
                Text = v_Publish.Context,
                Classification = v_Publish.Classification,
                IsPublic = v_Publish.IsPublic == 1 ? true : false,
                IsComment = v_Publish.IsPublic == 1 ? true : false,
                IsAllow = false
            };
            using (ArticeContext context = new ArticeContext())
            {
                context.Entry(article).State = Microsoft.EntityFrameworkCore.EntityState.Added;
                if (context.SaveChanges() == 1)
                {
                    return OperResult.Succeed();
                }
                return OperResult.Failed("发表失败");
            }
        }

        /// <summary>
        /// 封建用户文章
        /// </summary>
        /// <param name="token"></param>
        /// <param name="articleId"></param>
        /// <returns></returns>
        public bool DelArticle(int uid, int articleId)
        {
            using (ArticeContext content = new ArticeContext()) {
                var data = content.ArticleLists.FirstOrDefault(u => u.Id == articleId);
                if (data is null)
                    return false;
                if (data.ReleaseId != uid)
                    return false;

                data.IsAllow = true;
                content.Entry(data).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
                if (content.SaveChanges() is 1)
                    return true;
                return false;
            }
        }
    }
}
